# -*- coding:utf-8 -*- 
from google.appengine.ext import webapp
from models import cronjob
from google.appengine.api import taskqueue
from google.appengine.api import urlfetch
from google.appengine.api import mail
from google.appengine.ext.webapp import template
import datetime
class getjob(webapp.RequestHandler):
    def get(self):
        li=cronjob.all()
        for i in li:
            taskqueue.add(queue_name='default',url='/queuejob/', params={'joburl': i.watchurl,'email':i.email})
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')

class queuejob(webapp.RequestHandler):
    def get(self):
        url=self.request.get('joburl')
        result = urlfetch.fetch(url)
        if result.status_code == 200:
            pass
        else:
            sender_address = "Timger Support <Timgersupport@timgerjiankong.appspot.com>"
            subject = u"警告：你的网站访问出现问题"
            body = """警告我们的系统在监控你的网站过程中发现你的网站无法访问 请及时处理，故障时间是%s""" % str(datetime.datetime.now())
            mail.send_mail(sender_address, self.request.get('email'), subject, body)
            
class getinfo(webapp.RequestHandler):
    def get(self):
        import os
        path=os.path.join(os.getcwd(),'put.html')
        self.response.out.write(template.render(path, {}))
    def post(self):
        if self.request.get('joburl') and self.request.get('name') and self.request.get('email'):
            #self.response.out.write( '%s%s%s'%(self.request.get('joburl'), self.request.get('name') ,self.request.get('email')))
            c=cronjob.all().filter('name=',self.request.get('name')).get()
            d=cronjob.all().filter('joburl=',self.request.get('joburl')).get()
            e=cronjob.all().filter('email=',self.request.get('email')).get()
            if c or d or e:
                a=cronjob()
            #a.name='name'
            #a.watchurl='url'
            #a.email='haibolib@gmail.com'
                a.name=self.request.get('name')
                a.joburl=self.request.get('joburl')
                a.email=self.request.get('email')
                a.put()
                self.response.out.write('create job ok !!!')
                self.response.out.write('<p>yourname %s</p><p>you url :%s</p><p>you email:%s</p> ' %(a.name,a.joburl,a.email))
            else:
                self.response.out.write(u'输入的数据在数据库中已经存在，请修改后输入')
        else:
            self.response.out.write(u'数据不完整，请重新输入')
        
class guoweejob(webapp.RequestHandler):
    def get(self):
        loginurl='http://guowee.com:8080/login/'
        url='http://guowee.com:8080/top/'    
        import urllib
        login=urlfetch.fetch(loginurl,payload=urllib.urlencode({'email':'154693982@qq.com','pwd1':'11259375'}),method='POST',headers={})
        HEADER=login.headers
        d=dict(HEADER)
        COOKIE=d.get('set-cookie') 
        d['cookie']=COOKIE
        ACTIVE=urlfetch.fetch(url,method='GET',headers=d)
        import logging
        logging.info(ACTIVE.content)
        self.response.out.write(ACTIVE.content)